﻿using MSScriptControl;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Deployment;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public class GanjiLogin : BoundaryPostData
    {
        public string ganji_uuidJsFile = Application.StartupPath+"\\gjc.js";
        public GanjiLogin(CookieCollection ck)
            : base(ck)
        {

        }

        public Image StartLogin()
        {




            string o = "b()";
            //  string o="aaa('qqq')";
            string auuid = this.ExeJavascript(ganji_uuidJsFile, o);


            Cookie ck = new Cookie();
            ck.Name = "ganji_uuid";
            ck.Value = auuid;
            ck.Path = "/";
            ck.Domain = ".ganji.com";
            cklist.Add(ck);
            string gjss = @"https://passport.ganji.com/ajax.php?dir=captcha&module=login_captcha&nocache=1446526051";
            return GetImgFromUrl(gjss, ref this.cklist);
        }
        public Image GetImgFromUrl(string url, ref  CookieCollection cklist)
        {
            Image image;
            try
            {
                WebRequest webRequest = WebRequest.Create(url);
                HttpWebRequest request = webRequest as HttpWebRequest;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string mheader = response.Headers["Set-Cookie"].ToString();



                System.Collections.Hashtable hs = new System.Collections.Hashtable();

                string sa = mheader.Split(';')[0];






                Cookie c = new Cookie();
                c.Name = Regex.Replace(sa.Substring(0, sa.IndexOf('=')), @"\r\n", "").ToString();
                c.Value = Regex.Replace(sa.Substring(sa.IndexOf('=') + 1), @"\r\n", "").ToString();

                c.Domain = ".ganji.com";

                cklist.Add(c);
                Stream stream = response.GetResponseStream();
                stream.ReadTimeout = 1000;
                image = Image.FromStream(stream);

                stream.Close();
                response.Close();
            }
            catch (System.Exception ex)
            {
                this.LastStatus = ex.Message;

                return null;
            }
            this.LastStatus = "获取验证码成功";
            return image;
        }
        public void validationLogin(string checkCode,string username,string pwd)
        {



            
            string gj1u = "https://passport.ganji.com/login.php?next=/";
            string aou = doreqoutget(false, gj1u, "");
            string hspt = "window.PAGE_CONFIG.__hash__ = '([^\"]+)';";
            string hsout = this.getBysrcAndpattern(aou, hspt);

            ScriptControlClass js = new ScriptControlClass();
            js.Language = "javascript";
            var at = Math.Round(decimal.Parse(js.Eval(" new Date().getTime();").ToString()), 0);
            string loggj = @"https://passport.ganji.com/login.php?callback=jQuery18204047201075591147_" + ((long)at - new Random().Next(2000000, 3000000)).ToString() + "&username="+username+"&password="+pwd+"&checkCode=" + checkCode + "&setcookie=14&second=&parentfunc=&redirect_in_iframe=&next=%2F&__hash__=" + hsout + "&_=" + at;

            string loggjurl = @"https://passport.ganji.com/login.php";
          //  string postdata = "callback=jQuery18204047201075591147_" + ((long)at - new Random().Next(2000000, 3000000)).ToString() + "&username="+username+"&password="+pwd+"&checkCode=" + checkCode + "&setcookie=14&second=&parentfunc=&redirect_in_iframe=&next=%2F&__hash__=" + hsout + "&_=" + at;
            //string myou = doreqout(false, loggjurl, postdata);
            string myou = doreqoutget(false, loggj, "");
            ;


            if (cklist.Count > 5)
            {
                this.LastStatus = "登陆成功";

            }
            else
            {

                this.LastStatus = "请重新获取验证码";


            }
            //string mypo = "http://www.ganji.com/vip/my_post_list.php";
            //string poout = doreqoutget(true, mypo, "");

            //richTextBox1.Text = poout;
            //var a = 0;
        }
    }
}
